Add sobel_edges to keras.ops.image#22334
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances Keras's image processing capabilities by adding a robust Sobel edge detection function. This new utility allows users to easily identify edges in images across various backends and input configurations, providing a fundamental tool for computer vision tasks. The implementation ensures consistent behavior and output structure, streamlining image gradient computations within the Keras ecosystem. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces keras.ops.image.sobel_edges for Sobel edge detection, with implementations for JAX, NumPy, PyTorch, and TensorFlow backends. The changes are well-structured, including appropriate docstrings and tests. My feedback includes minor readability improvements for the manual Sobel filter implementations and suggestions to enhance test coverage for more robust validation.
Note: Security Review did not run due to the size of the PR.
| # Test edge detection on known pattern: vertical edge | ||
| backend.set_image_data_format("channels_last") | ||
| img = np.zeros((1, 5, 5, 1), dtype="float32") | ||
| img[0, :, 3:, 0] = 1.0 | ||
| result = kimage.sobel_edges(img, data_format="channels_last") | ||
| dx = result[0, :, :, 0, 1] | ||
| # The horizontal gradient should be non-zero near the edge | ||
| self.assertTrue(np.any(np.abs(np.array(dx)) > 0)) |
There was a problem hiding this comment.
To make this test more robust, you could also assert that the vertical gradient dy is zero for a vertical edge. Additionally, adding a similar test case for a horizontal edge would improve test coverage by verifying both dx and dy responses for basic patterns.
# Test edge detection on known pattern: vertical edge
backend.set_image_data_format("channels_last")
img = np.zeros((1, 5, 5, 1), dtype="float32")
img[0, :, 3:, 0] = 1.0
result = kimage.sobel_edges(img, data_format="channels_last")
dy = result[0, :, :, 0, 0]
dx = result[0, :, :, 0, 1]
# The horizontal gradient should be non-zero near the edge.
self.assertTrue(np.any(np.abs(np.array(dx)) > 0))
# The vertical gradient should be zero for a vertical edge.
self.assertAllClose(dy, np.zeros_like(dy))
# Test edge detection on known pattern: horizontal edge
img = np.zeros((1, 5, 5, 1), dtype="float32")
img[0, 3:, :, 0] = 1.0
result = kimage.sobel_edges(img, data_format="channels_last")
dy = result[0, :, :, 0, 0]
dx = result[0, :, :, 0, 1]
# The vertical gradient should be non-zero near the edge.
self.assertTrue(np.any(np.abs(np.array(dy)) > 0))
# The horizontal gradient should be zero for a horizontal edge.
self.assertAllClose(dx, np.zeros_like(dx))Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #22334 +/- ##
========================================
Coverage 82.85% 82.86%
========================================
Files 594 594
Lines 65732 65837 +105
Branches 10266 10288 +22
========================================
+ Hits 54462 54555 +93
- Misses 8650 8657 +7
- Partials 2620 2625 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Adds
keras.ops.image.sobel_edgesfor computing Sobel edge detection on images. Applies horizontal and vertical Sobel filters, appending a trailing dimension of size 2 (dy and dx gradients). Supports all backends (NumPy, TensorFlow, JAX, Torch; OpenVINO raises NotImplementedError), both data formats, and 3D/4D inputs.Fixes #22148